package com.mypofolio.model;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Properties;

import javax.swing.table.AbstractTableModel;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;

/**
 * @author  Paolo Cappelletti (Gempliss Srl)
 */
public class TableModel extends AbstractTableModel{
	
	private ArrayList<StockItem> stocks;
	
	public void init() throws FileNotFoundException, IOException, ConfigurationException{
		stocks=new ArrayList<StockItem>();		
		
		XMLConfiguration config = new XMLConfiguration("D:\\Lavoro\\Progetti\\_workspace\\MyPoFolio\\src\\stock.xml");
		System.out.println(config.getString("colors.background"));


		
		
		Properties props=new Properties();
		
		props.load(ClassLoader.getSystemResource("stock.properties").openStream());
		for (Iterator iterator = props.keySet().iterator(); iterator.hasNext();) {
			String key= (String) iterator.next();
			stocks.add(new StockItem(props.getProperty(key)));			
		}
	}	

	@Override
	public int getColumnCount() {
		return 5;
	}	

	@Override
	public int getRowCount() {
		return stocks.size();
	}	

	@Override
	public Object getValueAt(int row, int column) {
		if (column==0){
			return stocks.get(row).label;
		}else if (column==1){
			return stocks.get(row).quantity;
		}else if (column==2){
			return stocks.get(row).price;
		}else if (column==3){
			return stocks.get(row).quantity * stocks.get(row).price;
		}else if (column==4){
			return stocks.get(row).lastUpdate;
		}
		return "";
	}	
	
	@Override
	public String getColumnName(int column) {
		if (column==0){
			return "Stock name";
		}else 	if (column==1){
			return "Quantity";
		}else 	if (column==2){
			return "Price";
		}else 	if (column==3){
			return "Total";
		}else 	if (column==4){
			return "Last update";
		}
		
		return super.getColumnName(column);
	}

}
